package com.gpframework.module.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gpframework.common.constant.CacheConstants;
import com.gpframework.common.core.redis.RedisCache;
import com.gpframework.common.exception.BaseException;
import com.gpframework.module.common.domain.CommonWxAppletInfo;
import com.gpframework.module.common.domain.req.CommonWxAppletInfoReq;
import com.gpframework.module.common.domain.vo.CommonWxAppletInfoVo;
import com.gpframework.module.common.mapper.CommonWxAppletInfoMapper;
import com.gpframework.module.common.service.ICommonWxAppletInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName CommonWxAppletInfo
 * @Author gpframework
 * @Date 2022-04-07
 * @Version 1.0.0
 * @Description 微信小程序信息Service业务层处理
 */
@Service
public class CommonWxAppletInfoServiceImpl extends ServiceImpl<CommonWxAppletInfoMapper, CommonWxAppletInfo> implements ICommonWxAppletInfoService {
    protected final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private CommonWxAppletInfoMapper commonWxAppletInfoMapper;

    /**
     * redis缓存
     */
    @Autowired
    private RedisCache redisCache;

    /**
     * @Description 查询列表返回VO 用于返回给前端的列表接口
     * @Author gpframework
     * @Date 2022-04-07
     * @Param [lqw]
     * @Return List<CommonWxAppletInfoVo>
     */
    @Override
    public List<CommonWxAppletInfoVo> listVo(QueryWrapper<CommonWxAppletInfo> lqw) {
        return this.baseMapper.listVo(lqw);
    }

    /**
     * @Description 通过查询详情VO 用于返回给前端的列详情接口
     * @Author gpframework
     * @Date 2022-04-07
     * @Param [id]
     * @Return CommonWxAppletInfoVo
     */
    @Override
    public CommonWxAppletInfoVo getVoById(Long id) {
        return this.baseMapper.getVo(id);
    }


    /**
     * 获取系统信息（获取缓存）
     *
     * @param wxType
     * @return
     */
    @Override
    public CommonWxAppletInfoVo getSysInfo(String wxType) {
        //微信信息
        CommonWxAppletInfoVo commonWxAppletInfoVo;

        //系统微信信息
        Object appleObj = redisCache.getCacheObject(wxType);
        //没有系统信息时获取系统系统信息
        if (StringUtils.isEmpty(appleObj)) {
            CommonWxAppletInfoReq commonWxAppletInfoReq = new CommonWxAppletInfoReq();
            commonWxAppletInfoReq.setBrandId(0L);
            commonWxAppletInfoReq.setWxType(wxType);
            QueryWrapper<CommonWxAppletInfo> lqw = new QueryWrapper<>();
            commonWxAppletInfoReq.generatorQuery(lqw, true);
            List<CommonWxAppletInfoVo> commonWxAppletInfoVoList = this.baseMapper.listVo(lqw);
            if (commonWxAppletInfoVoList.size() <= 0) {
                return null;
            }
            commonWxAppletInfoVo = commonWxAppletInfoVoList.get(0);
            redisCache.setCacheObject(wxType, commonWxAppletInfoVo);
            return commonWxAppletInfoVo;
        }
        ObjectMapper objectMapper = new ObjectMapper();
        commonWxAppletInfoVo = objectMapper.convertValue(appleObj, CommonWxAppletInfoVo.class);
        return commonWxAppletInfoVo;
    }



    /**
     * 根据品牌id和微信类型获取品牌微信配置信息
     *
     * @param wxAppletInfoReq
     * @return
     */
    @Override
    public CommonWxAppletInfoVo findByBrandIdAndWxType(CommonWxAppletInfoReq wxAppletInfoReq) {
        if (StringUtils.isEmpty(wxAppletInfoReq.getBrandId()) || StringUtils.isEmpty(wxAppletInfoReq.getWxType())) {
            logger.info("----------品牌id或微信类型为空:" + wxAppletInfoReq + "----------");
            throw new BaseException("品牌id或微信类型为空");
        }
        String key = CacheConstants.BRAND_WX_INFO + wxAppletInfoReq.getWxType() + wxAppletInfoReq.getBrandId();
        CommonWxAppletInfoVo wxAppletInfoVo = redisCache.getCacheObject(key);
        //没有缓存
        if (StringUtils.isEmpty(wxAppletInfoVo)) {
            //查询配置信息
            wxAppletInfoVo = this.baseMapper.findByBrandIdAndWxType(wxAppletInfoReq);
            //设置缓存
            if (!StringUtils.isEmpty(wxAppletInfoVo)) {
                redisCache.setCacheObject(key, wxAppletInfoVo);
                redisCache.expire(key, 1, TimeUnit.DAYS);
            }
        }
        return wxAppletInfoVo;
    }
}
